/***************************************************************************
*   Copyright (C) 2010-2011 by swkyer <swkyer@gmail.com>                  *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#ifndef __JLINKMIPS_H__
#define __JLINKMIPS_H__

#include "Ejtag.h"
#include "CpuInfo.h"
#include "BreakPoint.h"
#include "Memory.h"
#include "Execution.h"


typedef enum __rsttype
{
	RST_TYPE_NORMAL = 0,
	RST_TYPE_HALT = 1,
} rsttype_t;


typedef struct __mips32_regs
{
	uint32_t greg[32];

	uint32_t sr;
	uint32_t lo;
	uint32_t hi;
	uint32_t bad;
	uint32_t cause;
	uint32_t pc;
} mips32_regs_t;


#define CORE_STATE_UNKNOW		0x0000
#define CORE_STATE_RUNNING		0x0001
#define CORE_STATE_HALTED		0x0002
#define CORE_STATE_AFTER_RESET	0x8000


typedef struct __mips32
{
	ejtag_t *ejtag;

	uint32_t target_state;
	mips32_cpuinfo_t cpuinfo;
	mips32_regs_t regs;

    mips32_bpt_info_t bpinfo;

    mips32_exec_context_t execontext;
} mips32_t;


JLINKMIPS_API mips32_t *mips32_init(ejtag_t *pejtag);
JLINKMIPS_API int mips32_exit(mips32_t *pmips32);


JLINKMIPS_API uint32_t mips32_get_state(mips32_t *pmips32);


JLINKMIPS_API int mips32_reset(mips32_t *pmips32, rsttype_t type);
JLINKMIPS_API int mips32_halt(mips32_t *pmips32, int probe);
JLINKMIPS_API int mips32_run(mips32_t *pmips32, ubase_t *paddr);
JLINKMIPS_API int mips32_step(mips32_t *pmips32, ubase_t *paddr);


JLINKMIPS_API int mips32_read_regs(mips32_t *pmips32);
JLINKMIPS_API int mips32_write_regs(mips32_t *pmips32);


JLINKMIPS_API int mips32_add_bpt(mips32_t *pmips32, mips32_bpt_t *pbpt);
JLINKMIPS_API int mips32_del_bpt(mips32_t *pmips32, mips32_bpt_t *pbpt);


JLINKMIPS_API int mips32_read_byte(mips32_t *pmips32, ubase_t addr, uint8_t *pbyte, int length);
JLINKMIPS_API int mips32_write_byte(mips32_t *pmips32, ubase_t addr, uint8_t *pbyte, int length);
JLINKMIPS_API int mips32_read_halfword(mips32_t *pmips32, ubase_t addr, uint16_t *pshort, int length);
JLINKMIPS_API int mips32_write_halfword(mips32_t *pmips32, ubase_t addr, uint16_t *pshort, int length);
JLINKMIPS_API int mips32_read_word(mips32_t *pmips32, ubase_t addr, uint32_t *intv, int length);
JLINKMIPS_API int mips32_write_word(mips32_t *pmips32, ubase_t addr, uint32_t *intv, int length);

JLINKMIPS_API int mips32_read_memory(mips32_t *pmips32, ubase_t addr, uint8_t *pbyte, int length);
JLINKMIPS_API int mips32_write_memory(mips32_t *pmips32, ubase_t addr, uint8_t *pbyte, int length);


#endif /* end of __JLINKMIPS_H__ */
